package com.mrd.frame.quartz;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.mrd.frame.attachment.AttachmentService;
import com.mrd.frame.domain.Order;
import com.mrd.frame.service.OrderService;

@Component("cleanImgTask")
public class CleanImgTask implements Job, Serializable {
    private static final long serialVersionUID = 1L;
    private Logger logger = Logger.getLogger(CleanImgTask.class);

    @Autowired
    private OrderService orderService;
    @Autowired
    private AttachmentService attachmentService;

    @Override
    public void execute(JobExecutionContext context) throws JobExecutionException {
    	//获取所有进行中的支付订单
    	Order record = new Order();
    	record.setStatus("1");
    	record.setTradeType("1");
    	List<Order> orderList = orderService.selectSelective(record);
    	if(orderList == null){
    		logger.info("没有进行中的支付订单");
    		return;
    	}
    	int deleteCount = 0;
    	//判断支付时间是否超过两小时
    	for(Order order : orderList){
    		Date now = new Date();
    		if((now.getTime() - order.getTimeStart().getTime()) > 1000L*60*60*2){
    			//更改状态为超时
    			order.setStatus("3");
    			orderService.updateByPrimaryKeySelective(order);
    			//清理对应的图片
    			attachmentService.delete(order.getOrderId());
    			deleteCount++;
    		}
    	}
    	logger.info("清理了" + deleteCount + "张图片");
        System.out.println("任务执行中");
    }
}
